package com.mes.inventory.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Page;
import com.mes.common.service.BasicService;
import com.mes.common.util.CodeUtil;
import com.mes.common.util.SysUtil;
import com.mes.inventory.dao.iface.IInvIODao;
import com.mes.inventory.dao.iface.IInvIODetailDao;
import com.mes.inventory.domain.InvIO;
import com.mes.inventory.domain.InvIODetail;
import com.mes.inventory.manager.InvInfoManager;

@Service
public class InvIOService extends BasicService<InvIO> {
	@Autowired
	CodeUtil codeUtil;
	
	@Autowired
	private IInvIODao invIODao;

	@Autowired
	private IInvIODetailDao invIODetailDao;

	@Autowired
	private InvInfoManager invlnfoManager;

	// 查询库房下的入库单带分页
	@DataProvider
	public void getInvIO(Page<InvIO> page, Map<String, Object> params) {
		if (params != null && !params.equals("")) {
			try {
				invIODao.getInvIOs(page, params);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	// 查询库房下的入库单
		@DataProvider
		public Collection<InvIO> getInvIOById(Map<String, Object> parameter) throws Exception {
			if (parameter != null && !parameter.equals("")) {
			String id = (String) parameter.get("uuid");
			InvIO invIO = invIODao.getById(id);
			List<InvIO> list = new ArrayList<InvIO>();
			list.add(invIO);
			return list;
			}
			return null;
		}

	// 主从表入库登账
	@DataResolver
	public void saveMaterial(List<InvIO> invIOs) throws Exception {
		invlnfoManager.updateInvInfo(invIOs);
	}

	// 非主从表保存入库单
	@DataResolver
	@Transactional
	public void saveInvIO(Collection<InvIO> invIOs, Collection<InvIODetail> invIODetails) throws Exception {
		this.saveData(invIOs);
		int i = 1;
		for (InvIODetail invIODetail : invIODetails) {
			invIODetail.setSeq(i);
			i++;
		}
		this.saveData(invIODetails);
	}

	// 非主从表入库登账
	@DataResolver
	@Transactional
	public void saveMaterial1(Collection<InvIO> invIOs, Collection<InvIODetail> invIODetails) throws Exception {
		this.saveData(invIOs);
		int i = 1;
		for (InvIODetail invIODetail : invIODetails) {
			invIODetail.setSeq(i);
			i++;
		}
		this.saveData(invIODetails);
		System.out.println(EntityUtils.getState(invIODetails));
		
		// invlnfoManager.updateInvInfo(invIOs);
		ArrayList<InvIO> list = new ArrayList<InvIO>();
		for (InvIO io : invIOs) {
			if(io.getIoState() == 5){
				throw new Exception("该入库单已提交，请不要重复提交！");
			}
			HashSet<InvIODetail> details = new HashSet<InvIODetail>();
			for (InvIODetail iod : invIODetails) {
				details.add(iod);
			}
			io.setDetails(details);
			// invlnfoManager.updateInvInfo(invIOs);
			list.add(io);
		}
		invlnfoManager.updateInvInfo(list);
	}
	
	//单条登账
	@DataResolver
	public void saveMaterial(InvIO invIO, Collection<InvIODetail> invIODetails) throws Exception {
		//System.out.println(EntityUtils.getState(invIODetails));
		ArrayList<InvIO> list = new ArrayList<InvIO>();
			if(invIO.getIoState() == 5){
				throw new Exception("该入库单已提交，请不要重复提交！");
			}
			HashSet<InvIODetail> details = new HashSet<InvIODetail>();
			for (InvIODetail iod : invIODetails) {
				details.add(iod);
			}
			invIO.setDetails(details);
			list.add(invIO);
			invlnfoManager.updateInvInfo(list);
		}
	
	// 保存出库单
	@DataResolver
	public void saveInvIOAndDetail(Collection<InvIO> invIOs, Collection<InvIODetail> invIODetails) throws Exception {
		for (InvIO io : invIOs) {
			HashSet<InvIODetail> details = new HashSet<InvIODetail>();
			for (InvIODetail iod : invIODetails) {
				iod.setUniqueId(SysUtil.getUUID());
				details.add(iod);
				invIODetailDao.add(iod);
			}
			io.setDetails(details);
			io.setUniqueId(SysUtil.getUUID());
			invIODao.add(io);
		}
	}
	
	/**
	 * 获得一个入库单号编码
	 * @return
	 * @throws Exception
	 */
	@Expose
	public String getInvInBillNumber() throws Exception {
		return codeUtil.getInvInBillNumber();
	}
	
	/**
	 * 获得一个出库单号编码
	 * @return
	 * @throws Exception
	 */
	@Expose
	public String getInvOutBillNumber() throws Exception {
		return codeUtil.getInvOutBillNumber();
	}
	
}
	
